package cn.com.taiji.oospub.wechat.common.tool;

import java.math.BigDecimal;
import java.text.DecimalFormat;

public class NumberTools {
	public static Double string2Double(String numberStr) {
		if (null == numberStr || "".equals(numberStr.trim())) {
			return null;
		}
		Double number = null;
		try {
			number = Double.valueOf(numberStr);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} 
		return number;
	}
	
	public static Integer string2Integer(String numberStr) {
		if (null == numberStr || "".equals(numberStr.trim())) {
			return null;
		}
		Integer number = null;
		try {
			number = Integer.valueOf(numberStr);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} 
		return number;
	}
	
	/**
	 * 得到百分比
	 * @param numerator	分子
	 * @param denominator	分母
	 * @return
	 */
	public static String getPercent(Double numerator, Double denominator) {
		if (numerator == null || denominator == null) {
			return "0";
		}
		double per = denominator == 0 ? 1 : numerator / denominator;
		return new DecimalFormat("#0.00").format(per * 100);
	}
	
	
	public static double divide(Double numerator, Double denominator) {
		if (numerator == null || denominator == null) {
			return 0;
		}
		double per = denominator == 0 ? 1 : numerator / denominator;
		return per;
	}
	
	public static Double format2Decimal(Double number) {
		if (number == null ) {
			return null;
		}
		BigDecimal b = new BigDecimal(number);
		return b.setScale(2,   BigDecimal.ROUND_HALF_UP).doubleValue();  
	}
	
	public static String format2Decimal2Str(Double value){
		DecimalFormat  df = new DecimalFormat("######0.00");   
		return df.format(value);
		
	}
	

}
